package com.tekj.cxqcb.view.Module.activity.ServiceOrder.fragment;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tekj.cxqcb.MainBActivity;
import com.tekj.cxqcb.R;
import com.tekj.cxqcb.operation.BusinessDao;
import com.tekj.cxqcb.operation.resultBean.ShopServiceOrder;
import com.tekj.cxqcb.view.Module.activity.ServiceOrder.OrderDetailsActivity;
import com.tekj.cxqcb.view.Module.activity.ServiceOrder.adapter.OrderListAdapter;
import com.tekj.cxqcb.view.Module.decoration.SpacesItemDecoration;

import butterknife.BindView;
import common.base.fragment.BaseFragment;
import common.base.operationHelper.Bean.BindingViewBean;

public class OrderListFragment extends BaseFragment {

    @BindView(R.id.rv_main)
    RecyclerView rvMain;
    @BindView(R.id.srl_main)
    SwipeRefreshLayout srlMain;

    OrderListAdapter listAdapter;
    BusinessDao businessDao;
    int p = 1;
    int type;

    @Override
    public void init() {

        type = getArguments().getInt("type", 0);

        businessDao = new BusinessDao(activity, this);

        listAdapter = new OrderListAdapter(null);
        rvMain.setLayoutManager(new LinearLayoutManager(activity));
        rvMain.setAdapter(listAdapter);
        rvMain.addItemDecoration(new SpacesItemDecoration(10));

        listAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(activity, OrderDetailsActivity.class);
                intent.putExtra("id",listAdapter.getData().get(position).getOrderId());
                startActivity(intent);
            }
        });
        listAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                p++;
                GetDataList();
            }
        }, rvMain);
        srlMain.setColorSchemeColors(getResources().getColor(R.color.colorPrimary));
        srlMain.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                p = 1;
                GetDataList();
            }
        });
        GetDataList();
    }

    void GetDataList() {
        businessDao.BShopServiceOrderList(MainBActivity.User.getShopId(), "", type, p);
    }

    @Override
    public int setLayout() {
        return R.layout.fragment_order_list;
    }

    @Override
    public void initLoadStart() {

    }

    @Override
    public void initLoadEnd() {

    }

    @Override
    public void initBindingView(Object mBean) {
        BindingViewBean bindingViewBean = (BindingViewBean) mBean;
        switch (bindingViewBean.getmEnumStatus()) {
            case 服务订单查询:
                ShopServiceOrder shopServiceOrder = (ShopServiceOrder) bindingViewBean.getBean();
                if (shopServiceOrder.getCode().equals("0000000")) {
                    if (p == 1) {
                        listAdapter.setNewData(shopServiceOrder.getData());
                    } else {
                        listAdapter.addData(shopServiceOrder.getData());
                    }
                    if (shopServiceOrder.getData().size() < 10) {
                        listAdapter.loadMoreEnd();
                    } else {
                        listAdapter.loadMoreComplete();
                    }

                }
                if (srlMain != null) {
                    srlMain.setRefreshing(false);
                }
                break;
        }


    }

}
